package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.vo.pagevo.InspectionManagementPageVo;
import com.hgxd.service.IInspectionManagementService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.List;

import com.hgxd.result.Result;

import com.hgxd.entity.pojo.InspectionManagement;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 巡检管理 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "巡检计划")
@RestController
@RequestMapping("/api/inspectionManagement")
@CrossOrigin
public class InspectionManagementController {
    @Resource(name = "InspectionManagementServiceImpl")
    private IInspectionManagementService inspectionManagementService;

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增巡检计划", notes = "传入巡检计划对象")
    @OperationLogAnnotation(operModule = "巡检计划管理-新增", operType = "新增", operContent = "新增巡检计划")
    public Result<Boolean> insert(@RequestBody InspectionManagement inspectionManagement) {
        return Result.ok(inspectionManagementService.save(inspectionManagement));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改巡检计划", notes = "传入修改的巡检计划对象，需传入id")
    @OperationLogAnnotation(operModule = "巡检计划管理-修改", operType = "修改", operContent = "修改某巡检计划")
    public Result<Boolean> update(@RequestBody InspectionManagement inspectionManagement) {
        return Result.ok(inspectionManagementService.updateById(inspectionManagement));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除", notes = "传入id")
    @OperationLogAnnotation(operModule = "巡检计划管理-根据id删除", operType = "删除", operContent = "删除某巡检计划")
    public Result<Boolean> delete(@PathVariable("id") Integer id) {
        return Result.ok(inspectionManagementService.removeById(id));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除", notes = "根据id批量删除")
    @OperationLogAnnotation(operModule = "巡检计划管理-批量删除", operType = "删除", operContent = "批量删除巡检计划")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(inspectionManagementService.removeByIds(ids));
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部", notes = "无需传参")
    @OperationLogAnnotation(operModule = "巡检计划管理-查询", operType = "查询", operContent = "查询所有巡检计划")
    public Result<List<InspectionManagement>> findAll() {
        return Result.ok(inspectionManagementService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @OperationLogAnnotation(operModule = "巡检计划管理-查询", operType = "查询", operContent = "查询某个巡检计划")
    public Result<InspectionManagement> findOne(@PathVariable("id") Integer id) {
        return Result.ok(inspectionManagementService.getById(id));
    }

    /**
     * 分页模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "需要传页码和大小，注意任务状态")
    @OperationLogAnnotation(operModule = "巡检计划管理-分页模糊查询", operType = "查询", operContent = "分页查询巡检计划")
    public Result<Page<InspectionManagement>> findPage(
            @RequestBody InspectionManagementPageVo inspectionManagementPageVo) {
        LambdaQueryWrapper<InspectionManagement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .between(inspectionManagementPageVo.getBeginTime() != null
                                && inspectionManagementPageVo.getEndingTime() != null
                        , InspectionManagement::getCreateTime
                        , inspectionManagementPageVo.getBeginTime()
                        , inspectionManagementPageVo.getEndingTime())
                .eq(!"".equals(inspectionManagementPageVo.getTaskName())
                        , InspectionManagement::getTaskName
                        , inspectionManagementPageVo.getTaskName())
                .eq(!"".equals(inspectionManagementPageVo.getStatus())
                        , InspectionManagement::getStatus
                        , inspectionManagementPageVo.getStatus());
        // 倒序查询
        queryWrapper.orderByDesc(InspectionManagement::getId);
        return Result.ok(inspectionManagementService.page(new Page<>(inspectionManagementPageVo.getPageNum()
                , inspectionManagementPageVo.getPageSize()), queryWrapper));
    }
}

